﻿'=======================================================================================================================================
'
'
' Applicaion:       Talking Speller
'
' Version:          1.00
'
' Written for:      Vision Australia - Training Department
' 
'
' Programmer:       Michael Williams
'
'
'
'
'
'=======================================================================================================================================
'Dependencies:
'=======================================================================================================================================
'               1) Add Speech to Visual Studio
'               Reference the System.Speech assembly from the application
'               Project | Add Reference...
'               In the Add Reference window, on the .NET tab, scroll until you find System.Speech and select it, and then click OK.    
'
'               In your Visual Studio project, add a using statement for each System.Speech namespace that you want to access. 
'               For example, if you want to work with speech synthesis, enter the following using statement
'               using System.Speech.Synthesis; 
'
'
'=======================================================================================================================================

' TODOMW: Include an option either in menu or button to change the lesson number.

' TODOMW: Need to be able to use Backspace in word edit box?

' TODOMW: Check the Tab order before production.

' TODOMW: Implement option to not speak the definition only the word and context.

'============================================================================================
' COMPILER OPTIONS
'============================================================================================
'The Visual Basic compiler provides three options that determine how it handles variables:
'    The Explicit option indicates whether you will declare all variables.
'    The Strict option indicates whether all variables will be of a specific type.
'    The Infer option indicates whether the compiler should determine the type of a variable
'    from its value.
'=====================================================================================================================


'=====================================================================================================================
' STD Options in every module should be.
'=====================================================================================================================
'Explicit On:   All variables must be declared.
'Strict On:     Requires all variables to have an As clause. Does not allow late binding.
'Infer Off:     Will not allow you to declare and initialize typed variables without specifying their type.
'
'----------------------------------------------------------------------------------------------------------------------
Option Explicit On
Option Strict On
Option Infer Off


Public Class Form1
    ' TODOMW: Enable with events if implemented later. //Dim WithEvents oSpellingLessonMW As New SpellingLessonMW
    Dim m_oSpellingLessonMW As New SpellingLessonMW

    ' TODOMW: Enable with events if implemented later. //Dim WithEvents as New SpeechMW
    Dim m_oSpeechMW As New SpeechMW
    Dim m_oSoundsMW As New SoundsMW


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' When the Application starts Focus to the Edit box that you imput the word to spell.
        WordToSpell.Focus()

        ' TODOMW: Add logic to disable button when pressed and enable the Stop and Pause buttons. etc.
        m_oSpeechMW.SayString(m_oSpellingLessonMW.GetWordContextAndDefinition(True))
    End Sub


    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        ' Place hotkeys for form here...

        ' Stop any current speaking if user types a key.
        m_oSpeechMW.SayStop()


        '=====================================
        ' CONTROL + C = Say Word in context.
        '=====================================
        If e.Control And e.KeyCode = Keys.C Then
            m_oSpeechMW.SayString("Context: " & m_oSpellingLessonMW.context)
            ' Stop key character being passed to focused control.
            e.Handled = True
            ' Stop Audible warning if focus is in a TextBox control.
            e.SuppressKeyPress = True
        End If

        '=====================================
        ' CONTROL + D = Say Word Definition.
        '=====================================
        If e.Control And e.KeyCode = Keys.D Then
            m_oSpeechMW.SayString("Definition: " & m_oSpellingLessonMW.definition)
            ' Stop key character being passed to focused control.
            e.Handled = True
            ' Stop Audible warning if focus is in a TextBox control.
            e.SuppressKeyPress = True
        End If

        '=====================================
        ' CONTROL + E = Move to type word edit box.
        '=====================================
        If e.Control And e.KeyCode = Keys.E Then
            WordToSpell.Focus()
        End If

        '==============================================
        ' CONTROL + H = Hint - Spell word back to USER.
        '==============================================
        If e.Control And e.KeyCode = Keys.H Then
            m_oSpeechMW.SayString("Hint: ")
            m_oSpeechMW.SayString(m_oSpellingLessonMW.word)
            m_oSpeechMW.SpellString(m_oSpellingLessonMW.word)
            m_oSpeechMW.SayString(m_oSpellingLessonMW.word)
            If Not IsNothing(m_oSpellingLessonMW.currentCharactersCorrect) Then
                ' Say the current characters correct so far.
                m_oSpeechMW.SayString("Entered so far.")
                m_oSpeechMW.SpellString(m_oSpellingLessonMW.currentCharactersCorrect)
            End If
            m_oSpellingLessonMW.hintGiven = True
            ' Stop key character being passed to focused control.
            e.Handled = True
            ' Stop Audible warning if focus is in a TextBox control.
            e.SuppressKeyPress = True
        End If

        '=====================================
        ' CONTROL + W = Say Word to Spell.
        '=====================================
        If e.Control And e.KeyCode = Keys.W Then
            ' Say to USER the word to spell 
            m_oSpeechMW.SayString("Spell: " & m_oSpellingLessonMW.word)
            ' Stop key character being passed to focused control.
            e.Handled = True
            ' Stop Audible warning if focus is in a TextBox control.
            e.SuppressKeyPress = True
        End If
    End Sub


    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        ' Stop the speech before exiting. Otherwise crashes.
        m_oSpeechMW.SayStop()
        Application.Exit()
    End Sub

    Private Sub WordToSpell_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles WordToSpell.KeyDown
        ' Say character that the USER presses.

        ' TODOMW: Add to Optional settings set by user. Turn ON/OFF keyboard typing character or none.

        If Not e.Alt Then
            ' Only say the character if a character. Do not say if Alt key combination like Alt+S.
            m_oSpeechMW.SayString(Convert.ToChar(e.KeyCode))
        End If
    End Sub



    Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click

        ' TODOMW: Add logic to disable button when pressed and enable the Stop and Pause buttons. etc.
        m_oSpeechMW.SayString(m_oSpellingLessonMW.GetWordContextAndDefinition(True))
    End Sub

    Private Sub WordToSpell_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles WordToSpell.KeyPress
        ' Check the characters that are typed in by USER to the word that needs spelling.

        If m_oSpellingLessonMW.IsCharacterCorrect(e.KeyChar) = True Then

            If m_oSpellingLessonMW.wordComplete = True Then
                ' Don't enter the last character of the word. As it does not get cleared with the Edit box clear command.
                e.Handled = True

                ' Clear the edit box word has been completed correctly.
                WordToSpell.Clear()

                ' TODOMW: have in menu optoins user defined affermatino after a correct word. Incorrect word also?
                ' Play sound correct.
                m_oSpeechMW.SayString("Correct.")

                ' Get next word in words to spell list.
                If m_oSpellingLessonMW.NextWord() = True Then
                    ' Tell USER the next word to spell.
                    m_oSpeechMW.SayString(m_oSpellingLessonMW.GetWordContextAndDefinition(True))
                Else
                    ' TODOMW: Temp code only. Delete later when next lesson logic added.
                    ' TODOMW: User defined - Word list complete message. In options menu.
                    m_oSpeechMW.SayString("Lesson complete. Congradulations!")

                    ' TODOMW: Finish code.
                    m_oSpellingLessonMW.NextLesson()
                End If
            End If
        Else
            ' Stop the character from speaking.
            m_oSpeechMW.SayStop()

            m_oSoundsMW.PlaySoundCharacterIncorrect()

            ' Don't input character into text box if not correct.
            e.Handled = True

            ' Say the word again.
            m_oSpeechMW.SayString("Spell: " & m_oSpellingLessonMW.word & ". ")
            If Not IsNothing(m_oSpellingLessonMW.currentCharactersCorrect) Then
                ' Say the current characters correct so far.
                m_oSpeechMW.SayString("Entered so far.")
                m_oSpeechMW.SpellString(m_oSpellingLessonMW.currentCharactersCorrect)
            End If
        End If

    End Sub
End Class
